﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using IceCS.DataStructure.Core.String;
using IceCS.DataStructure.Helpers;

namespace IceCS.DataStructure.One2HunderdQ
{
    public class Game : SystemBase
    {
        public QuestionReader QuestionReader { get; set; }
        ArrayList questionList;
        SequenceString answer;
        SequenceString tenanswer;
        int daren = 100;

        public Game()
        {
            QuestionReader = new QuestionReader();
            questionList = QuestionReader.GetQuestionList();
        }

        private void DisplayQuestion()
        {
            int score = 0;
            int correctdaren = 0;
            foreach (SequenceString[] ques in questionList)
            {
                Console.WriteLine("{0},{1}",ques[0],ques[1]);
                Console.WriteLine("{0}",ques[2]);
                Console.WriteLine();
                Console.WriteLine("请选择正确答案：");
                HundredA(daren);
                answer = new SequenceString(Console.ReadLine().ToCharArray());
                Console.WriteLine("正确答案是：{0}\n",ques[3]);
                int startpos = 0;
                int endpos = 0;
                do
                {
                    endpos = tenanswer.IndexOf(answer, startpos);
                    if (endpos==-1)
                    {
                        endpos = tenanswer.Length;
                    }
                    if (endpos!=tenanswer.Length)
                    {
                        correctdaren = correctdaren + 1;
                        
                    }
                    startpos = endpos + 1;

                } while (startpos < tenanswer.Length);
                if (answer.Compare(ques[3]) == 0)
                {
                    score += 100;
                    Console.WriteLine("恭喜你，答对了！你现在金球数已增至：{0}",score);
                    Console.WriteLine("现在答对题目的快乐答人数是：{0}",correctdaren);
                    if (correctdaren > 0)
                    {
                        daren = correctdaren;
                    }
                    else
                    {
                        Console.WriteLine("恭喜你获得了{0}个金球",score);
                        return;
                    }
                    correctdaren = 0;
                    Console.WriteLine("想要继续玩吗？");
                    var flag = ConsoleHelper.GetRightInputStringYN();
                    if (!flag)
                    {
                        return;
                    }
                }
                else
                {
                    Console.WriteLine("您的金球将被{0}个快乐达人瓜分",daren);
                    return;
                }
            }
        }

        private void HundredA(int daren)
        {
            tenanswer = new SequenceString(" ".ToCharArray());
            for (int i = 0; i < daren; i++)
            {
                Random random = new Random(new RNGCryptoServiceProvider().GetHashCode() * unchecked((int)DateTime.Now.Ticks));
                int numrand = random.Next(1, 4);
                switch (numrand)
                {
                    case 1:
                        {
                            var a = new SequenceString("A".ToCharArray());
                            tenanswer = tenanswer.Concat(a);
                            break;
                        }
                    case 2:
                        {
                            var a = new SequenceString("B".ToCharArray());
                            tenanswer = tenanswer.Concat(a);
                            break;
                        }
                    case 3:
                        {
                            var a = new SequenceString("C".ToCharArray());
                            tenanswer = tenanswer.Concat(a);
                            break;
                        }
                }
            }
        }

        protected override void InitStorageStructure()
        {

        }

        protected override void MainLoop()
        {
            DisplayQuestion();
        }

        
    }
}
